import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/login', component: () => import('@/views/Login/LoginPage.vue') },
    {
      path: '/afterSale',
      component: () => import('@/views/ServicePage/AfterSalePage.vue')
    },
    {
      path: '/helpCenter',
      component: () => import('@/views/ServicePage/HelpCenterPage.vue')
    },
    {
      path: '/serviceTerms',
      component: () => import('@/views/ServicePage/ServiceTermsPage.vue')
    },
    {
      path: '/deliverGoods',
      component: () => import('@/views/ServicePage/DeliverGoodsPage.vue')
    },
    {
      path: '/',
      component: () => import('@/layout/LayoutContainer.vue'),
      redirect: '/main',
      children: [
        { path: '/main', name: 'MainPage', component: () => import('@/views/MainPage.vue') },
        {
          path: '/main/:type',
          component: () => import('@/views/TypePage.vue')
        },
        {
          path: '/personinfomation',
          name: 'PersonInformationPage',
          component: () => import('@/views/Person/PersonInformationPage.vue')
        },
        {
          path: '/goodmanage/:id',
          name: 'Goods',
          component: () => import('@/views/Person/ManageGoodsPage.vue')
        },
        {
          path: '/productdetail/:id',
          name: 'Detail',
          component: () => import('@/views/Goods/ProductDetailsPage.vue')
        },
        {
          path: '/shopcard/:id',
          name: 'ShopCard',
          component: () => import('@/views/Person/ShopCardPage.vue')
        },
        {
          path: '/pay',
          component: () => import('@/views/Goods/PayPage.vue')
        },
        {
          path: '/search',
          name: 'Search',
          component: () => import('@/views/Goods/SearchPage.vue')
        }
      ]
    },
    { path: '/:catchAll(.*)', component: () => import('@/views/EmptyPage.vue') }
  ]
})

router.beforeEach((to, form) => {
  const userStore = useUserStore()
  if (
    !userStore.getToken() &&
    (to.path.startsWith('/personinfomation') ||
      to.path.startsWith('/goodmanage') ||
      to.path.startsWith('/shopcard') ||
      to.path.startsWith('/pay'))
  ) {
    ElMessage.error(`请登录用户帐号`)
    return '/login'
  }
})

router.afterEach(() => {
  const userStore = useUserStore()
  // 判断过期时间
  const expiration = localStorage.getItem('expiration')
  // 获取当前时间
  const nowTime = new Date().getTime()
  // 判断token是否过期
  if (expiration < nowTime) {
    localStorage.removeItem('token')
    localStorage.removeItem('user')
    localStorage.removeItem('expiration')
  }
  // 获取本地存储
  userStore.setToken(localStorage.getItem('token') || '')
  userStore.setUserInfo(
    JSON.parse(localStorage.getItem('user')) || {
      id: '',
      username: '',
      password: '',
      name: '',
      email: '',
      gender: '',
      birthday: '',
      phone: '',
      address: '',
      money: ''
    }
  )
})

export default router
